package com.example.demo.Controller;

import com.example.demo.common.AjaxResult;
import com.example.demo.common.SessionUtil;
import com.example.demo.common.Tool;
import com.example.demo.model.Account;
import com.example.demo.model.User;
import com.example.demo.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ling
 * Date: 2023-12-13
 * Time: 10:06
 */
@RestController
@RequestMapping("/account")
public class AccountController {



    @Autowired
    AccountService accountService;

    @Autowired
    private DataSourceTransactionManager dataSourceTransactionManager;

    @Autowired
    private TransactionDefinition transactionDefinition;
    @RequestMapping("/create")
    public Object create(HttpServletRequest request,String password, Integer money) {
        if(!StringUtils.hasLength(password) || money < 0) {
            return AjaxResult.fail(-1,"非法的参数请求");
        }
        User user = SessionUtil.getLoginUser(request);
        if(user == null) {
            return AjaxResult.fail(-1,"找不到session");
        }
        System.out.println("红红火火恍恍惚惚哈哈哈哈哈哈哈哈哈哈哈"+user.getId());
        int num = Tool.number;
        int result = accountService.create(num,money,password,user.getId());
        Tool.number++;
        if(result == 1) {
            return AjaxResult.success(num,"添加成功");
        } else {
            return AjaxResult.fail(-1,"数据添加错误");
        }
    }

    @RequestMapping("/selectall")
    public List<Account> selectAll(HttpServletRequest request) {
        User user = SessionUtil.getLoginUser(request);
        if(user == null) {
            return null;
        }
        List<Account> accounts = accountService.selectAll(user.getId());
        return accounts;
    }

    @RequestMapping("/selectbynum")
    public Account selectByNum(Integer number) {
        Account account = accountService.selectAccountByNumber(number);
        return account;
    }

    @RequestMapping("/transfer")
    public Object transferAccount(HttpServletRequest request,Integer num1,Integer num2,Integer money) {
        //num1转给num2
        User user = SessionUtil.getLoginUser(request);
        Account account1 = accountService.selectAccountByNumber(num1);
        Account account2 = accountService.selectAccountByNumber(num2);
        if(account1 == null || account2 == null || account1.getUserId() != user.getId() || money < 0) {
            return AjaxResult.fail(-1,"非法的参数请求");
        }
        if(account1.getMoney() < money) {
            return AjaxResult.fail(-1,"余额不足");
        }
        TransactionStatus transactionStatus =  dataSourceTransactionManager.getTransaction(transactionDefinition);
        int result1 = accountService.reduceMoney(num1,money);
        int result2 = accountService.addMoney(num2,money);
        dataSourceTransactionManager.commit(transactionStatus);
        return AjaxResult.success(1,"转账成功");
    }




}
